package com.sky.wonderrelationship.controller;

import com.sky.wonderrelationship.entity.Field;
import com.sky.wonderrelationship.entity.FieldShipDetail;
import com.sky.wonderrelationship.entity.FieldShipDetailList;
import com.sky.wonderrelationship.entity.TreeRelationshipsNode;
import com.sky.wonderrelationship.service.FieldShipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 功能很简单，一个controller就够了
 *
 * @author: lx
 * @date: 2023年04月14日 14:20
 */
@RestController
public class AllController {
    @Autowired
    FieldShipService fieldShipService;

    @GetMapping("/search")
    public List<Field> search() {
        return fieldShipService.getAllFied();
    }


    //模糊查找
    @GetMapping("/fuzzySearchFied")
    public List<Field> fuzzySearchFied(String db, String table, Long field) {
        return fieldShipService.fuzzySearchField(db, table, field);
    }

    //按列id查询关系
    @GetMapping("/getRelationshipByFieldId")
    public List<FieldShipDetail> getRelationshipByfieldId(@RequestParam("id") Long field) {
        return fieldShipService.getRelationshipByfieldId(field);
    }

    //增加关系
    @PostMapping("/addRelationShipByFieldId")
    public Integer addRelationShipByFieldId(Long field1, Long field2,String note) {
        return fieldShipService.addRelationShipByFieldId(field1,field2,note);
    }

    //删除关系
    @PostMapping("/deleteRelationShipByFieldId")
    public Integer deleteRelationShipByFieldId(Long id) {
        return fieldShipService.deleteRelationShipByFieldId(id);
    }


    //按库，表，字段来查关联关系
    @PostMapping ("/seachSelectShip")
    public List<FieldShipDetailList> seachSelectShip(@RequestParam("db")String db, @RequestParam("table")String table, @RequestParam(value = "field",required = false)Long field) {
        //去除没有关联关系的字段
        List<FieldShipDetailList> list = fieldShipService.seachSelectShip(db, table, field).stream().filter(fieldShipDetailList -> fieldShipDetailList.getFieldShipDetailList().size()!=0).collect(Collectors.toList());
        return list;
    }


/*    //按库，表，字段来查关联关系（另一种写法）
    @PostMapping ("/seachSelectShip")
    public List<FieldShipDetailList> seachSelectShip(@RequestParam("db")String db, @RequestParam("table")String table) {
               return list;
    }*/

    //按库和表 以及可能存在的field来查找有关联的表    结果返回森林或者树。
    @PostMapping("/findRelationship")
    public  List<String> seachSelectShip(@RequestBody Map<String,String> map) {
        //起始点。db和table都不能为空
        Field field1 = new Field(map.get("db1"),map.get("table1"),map.get("field1"));
        //终点， 为需要找到的关系，field2可以为空，别的不能为空
        Field field2 = new Field(map.get("db2"),map.get("table2"),map.get("field2"));

        //没有详细修剪过的森林，包含了不能到达要求的节点
        List<TreeRelationshipsNode> list =  fieldShipService.queryTableAndTableShip(field1,field2);

        //对上面标记森林中有用的节点
        List<TreeRelationshipsNode> forestList = fieldShipService.makeTagForest(list,field2);

        //因为不确定树的层数，最好就别要前端来解析，后端直接转换为字符串拼接。
        List<String> strings = fieldShipService.forestToString(forestList);
        return strings;
    }

}
